﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class ChiTietHoaDonTraAdd : System.Web.UI.Page
    {
       static IUnitOfWork iunitfwork = new EFUnitOfWork();
       static CHITIETHOADONTRARepository repositoryCHitiet = new CHITIETHOADONTRARepository(new EFRepository<CHITIETHOADONTRA>(), iunitfwork);
       static HOADONTRARepository repository = new HOADONTRARepository(new EFRepository<HOADONTRA>(), iunitfwork);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadMatHang();
                LoadNhomMatHang();
                LoadHienThi();
                LoadMahdn();
            }

        }
        public void LoadHienThi()
        {
            
            string strid = "" + Request["MAHDT"];
            CHITIETHOADONTRA objChitiethoadontra = null;
            if (!string.IsNullOrEmpty(strid))
            {
                objChitiethoadontra = repositoryCHitiet.GetById(strid);
                txtmahdtra.Value = objChitiethoadontra.MAHDT;
                Dlnhacc.SelectedValue = objChitiethoadontra.HOADONTRA.NHACUNGCAP.MANCC;
                Dlmathang.SelectedValue = objChitiethoadontra.MATHANG.MAMH;
                txtsoluong.Value = objChitiethoadontra.SOLUONGTRA.ToString();
                Dlmahdn.SelectedValue = objChitiethoadontra.HOADONTRA.HOADONNHAP.MAHDN;
            }
        }
        /// <summary>
        /// Load nhà cung cấp
        /// </summary>
        public void LoadNhomMatHang()
        {
            NHACUNGCAPRepository repository = new NHACUNGCAPRepository(new EFRepository<NHACUNGCAP>(), iunitfwork);
            IQueryable<NHACUNGCAP> objNhacungcap = repository.All();
            Dlnhacc.DataSource = objNhacungcap;
            Dlnhacc.DataValueField = "MANCC";
            Dlnhacc.DataTextField = "TENNCC";
            Dlnhacc.DataBind();
            Dlnhacc.Items.Insert(0, new ListItem { Text = "---Chọn nhà cung cấp---", Value = "0" });
        }
        public void LoadMahdn()
        {
            HOADONNHAPRepository repository = new HOADONNHAPRepository(new EFRepository<HOADONNHAP>(), iunitfwork);
            IQueryable<HOADONNHAP> objmahd = repository.All();
            Dlmahdn.DataSource = objmahd;
            Dlmahdn.DataValueField = "MAHDN";
            Dlmahdn.DataValueField = "MAHDN";
            Dlmahdn.DataBind();
           Dlmahdn.Items.Insert(0,new ListItem{Text="--Chọn mã hóa đơn nhập---", Value="0"});
        }
        /// <summary>
        /// Load mặt hàng
        /// </summary>
        public void LoadMatHang()
        {
            MATHANGRepository repository = new MATHANGRepository(new EFRepository<MATHANG>(), iunitfwork);
            IQueryable<MATHANG> objMathang = repository.All();
            Dlmathang.DataSource = objMathang;
            Dlmathang.DataValueField = "MAMH";
            Dlmathang.DataTextField = "TENMH";
            Dlmathang.DataBind();
            Dlmathang.Items.Insert(0, new ListItem { Text = "---Chọn mặt hàng---", Value = "0" });
        }
        protected void Dlnhacc_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        public void capnhap()
        {
            string strid = "" + Request["MAHDT"];
            CHITIETHOADONTRA objChitiethoadontra = null;
            HOADONTRA objhoadontra = null;
            bool iscapnhap = true;
            if (!string.IsNullOrEmpty(strid))
            {
                objChitiethoadontra = repositoryCHitiet.GetById(strid);
                objhoadontra = repository.GetById(strid);
                iscapnhap = false;

            }
            else
            {
                objhoadontra = new HOADONTRA();
                objChitiethoadontra=new CHITIETHOADONTRA();
                iscapnhap = true;
            }
            if (string.IsNullOrEmpty(txtmahdtra.Value) || Dlmathang.SelectedValue.Equals("0") || Dlnhacc.SelectedValue.Equals("0"))
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert ('Vui lòng nhập đầy đủ')</script>");
                return;
            }
            objhoadontra.MAHDN = txtmahdtra.Value;
            objhoadontra.MANCC = Dlnhacc.SelectedValue;
            objhoadontra.MAHDN = Dlmahdn.SelectedValue;
            objhoadontra.NGAYTRA = DateTime.Now;
            objhoadontra.GHICHU = txtghichu.Value;

            objChitiethoadontra.MAHDT = txtmahdtra.Value;
            objChitiethoadontra.MAMH = Dlmathang.SelectedValue;
            objChitiethoadontra.SOLUONGTRA = int.Parse(txtsoluong.Value);
            objChitiethoadontra.TONGTIENTRA = int.Parse(txtsoluong.Value) * objChitiethoadontra.HOADONTRA.HOADONNHAP.DONGIA;
            try
            {
                if (iscapnhap)
                {
                    repository.Add(objhoadontra);
                }
                repository.Save();
                if (iscapnhap)
                {
                    repositoryCHitiet.Add(objChitiethoadontra);
                }
                repositoryCHitiet.Save();

            }
            catch (Exception ex)
            {
                Response.Write("<Script> alert('Lỗi rồi') </Script>");
                Response.Redirect("ChiTietHoaDonTraAdd.aspx?MAKHO=" + strid);
            }

        }

        protected void lbtncapnhap_Click(object sender, EventArgs e)
        {
            capnhap();
        }

        protected void lbtnthemmoi_Click(object sender, EventArgs e)
        {
            txtghichu.Value = "";
            txtmahdtra.Value = "";
            txtsoluong.Value = "";
            Dlmahdn.SelectedValue = "0";
            Dlmathang.SelectedValue = "0";
            Dlnhacc.SelectedValue = "0";
            Response.Redirect("ChiTietHoaDonTraAdd.aspx");
        }
    }
}